package com.kelvin.paopao.module.market.wallet.detail;

import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.WalletDetailModel;
import com.kelvin.paopao.module.base.IBasePresenter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/23.
 */

public class WalletDetailPresenter implements IBasePresenter {
    private WalletDetailActivity mView;

    public WalletDetailPresenter(WalletDetailActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(boolean isRefresh) {
        Observable.zip(RetrofitService.getAccount("+"), RetrofitService.getAccount("-"),
                new BiFunction<CommonListResponse<WalletDetailModel>, CommonListResponse<WalletDetailModel>, CommonListResponse<WalletDetailModel>>() {

                    @Override
                    public CommonListResponse<WalletDetailModel> apply(@NonNull CommonListResponse<WalletDetailModel> walletDetailModelCommonListResponse, @NonNull CommonListResponse<WalletDetailModel> walletDetailModelCommonListResponse2) throws Exception {
                        CommonListResponse response = new CommonListResponse();
                        List<WalletDetailModel> results = new ArrayList<>();
                        results.addAll(walletDetailModelCommonListResponse.getResult());
                        results.addAll(walletDetailModelCommonListResponse2.getResult());
                        Collections.sort(results, new Comparator<WalletDetailModel>() {
                            @Override
                            public int compare(WalletDetailModel o1, WalletDetailModel o2) {
                                return (int) (CommonUtil.formateRelativTime(o2.getAddtime()) - CommonUtil.
                                        formateRelativTime(o1.getAddtime()));
                            }
                        });
                        response.setResult(results);
                        return response;
                    }
                }).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mView.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<WalletDetailModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<WalletDetailModel> walletDetailModelCommonListResponse) throws Exception {
                        mView.hideLoading();
                        mView.loadData(walletDetailModelCommonListResponse.getResult());
                    }
                });

    }

    @Override
    public void getMoreData() {

    }
}
